package com.zhaojonhy.zhaopangwb.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhaojonhy.zhaopangwb.R;

/**
 * Created by Administrator on 2016/9/5.
 */
public class TitleBuilder {
    private View view ;
    private TextView tvTitle ;
    private ImageView ivLeft ;
    private ImageView ivRight ;
    private TextView tvLeft ;
    private TextView tvRight ;

    public TitleBuilder(Activity context) {
        view = context.findViewById(R.id.rl_titlebar) ;
        tvTitle = (TextView)view.findViewById(R.id.titleBarTv) ;
        ivLeft = (ImageView)view.findViewById(R.id.titleBarIvLeft) ;
        ivRight = (ImageView)view.findViewById(R.id.titleBarIvRight) ;
        tvLeft = (TextView)view.findViewById(R.id.titleBarTvLeft) ;
        tvRight = (TextView)view.findViewById(R.id.titleBarTvRight) ;
    }

    public TitleBuilder(View context) {
        view = context.findViewById(R.id.rl_titlebar) ;
        tvTitle = (TextView)view.findViewById(R.id.titleBarTv) ;
        ivLeft = (ImageView)view.findViewById(R.id.titleBarIvLeft) ;
        ivRight = (ImageView)view.findViewById(R.id.titleBarIvRight) ;
        tvLeft = (TextView)view.findViewById(R.id.titleBarTvLeft) ;
        tvRight = (TextView)view.findViewById(R.id.titleBarTvRight) ;
    }

    //Title
    //标题栏背景设置
    public TitleBuilder setTitleBgRes(int resId) {
        tvTitle.setBackgroundResource(resId);
        return this ;
    }

    //标题栏文字设置
    public TitleBuilder setTitleText(String text){
        tvTitle.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
        tvTitle.setText(text);
        return this ;
    }

    //left
    //设置图片按钮
    public TitleBuilder setLeftImage(int resId){
        ivLeft.setVisibility(resId>0 ? View.VISIBLE : View.GONE);
        ivLeft.setImageResource(resId);
        return this ;
    }

    //设置文字
    public TitleBuilder setLeftText(String text){
        tvLeft.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
        tvLeft.setText(text);
        return this ;
    }


    //right
    //设置图片按钮
    public TitleBuilder setRightImage(int resId){
        ivRight.setVisibility(resId>0 ? View.VISIBLE : View.GONE);
        ivRight.setImageResource(resId);
        return this ;
    }

    //设置文字
    public TitleBuilder setRightText(String text){
        tvRight.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
        tvRight.setText(text);
        return this ;
    }

    //设置监听事件
    //判断显示图片按钮还是文字按钮
    public TitleBuilder setLeftOnClickListener(View.OnClickListener listener){
        if(ivLeft.getVisibility() == View.VISIBLE){
            ivLeft.setOnClickListener(listener);
        }else if(tvLeft.getVisibility() == View.VISIBLE){
            tvLeft.setOnClickListener(listener);
        }
        return this ;
    }

    //设置监听事件
    //判断显示图片按钮还是文字按钮
    public TitleBuilder setRightOnClickListener(View.OnClickListener listener){
        if(ivRight.getVisibility() == View.VISIBLE){
            ivRight.setOnClickListener(listener);
        }else if(tvRight.getVisibility() == View.VISIBLE){
            tvRight.setOnClickListener(listener);
        }
        return this ;
    }

    public View build() {
        return view;
    }


}
